package com.roamer.audition.server.query.base;

import com.roamer.audition.server.entity.base.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * 分页查询
 *
 * @author roamer
 * @version V1.0
 * @date 2019/9/2 1:23 下午
 */
@ApiModel(value = "分页查询Query")
@EqualsAndHashCode(callSuper = true)
@Data
public abstract class PageQuery<T extends BaseEntity> extends BaseQuery<T> {

    private static final long serialVersionUID = 2117132504621363423L;

    @ApiModelProperty(value = "页码", notes = "从0开始分页")
    protected int page = 0;

    @ApiModelProperty(value = "分页大小", notes = "每页大小")
    protected int size = 10;

    /**
     * JPA分页查询类
     *
     * @return {@code Pageable}
     */
    public Pageable toPageable() {
        return PageRequest.of(page, size);
    }

    /**
     * JPA分页查询类,带排序条件
     *
     * @param sort 排序条件
     *
     * @return {@code Pageable}
     */
    public Pageable toPageable(Sort sort) {
        return PageRequest.of(page, size, sort);
    }
}
